//
//  GZLaunchController.m
//  GongZheng
//
//  Created by huan on 2018/4/27.
//  Copyright © 2018年 huan. All rights reserved.
//

#import "GZLaunchController.h"

@interface GZLaunchController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *mainView;

@property (nonatomic, strong) UIPageControl *page;

@end

@implementation GZLaunchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navBar.hidden = YES;
    self.notShowLeft = YES;
    [self setUpUI];
}

- (void)setUpUI{
    self.page.frame = CGRectMake(0, SCREEN_HEIGHT - 30, SCREEN_WIDTH, 10);
    self.page.centerX = self.view.centerX;
    [self.view addSubview:self.mainView];
    [self.view addSubview:self.page];
}


- (void)dealloc {
    
}

#pragma mark - scrollview delegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int page = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.page.currentPage = page;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    int cuttentIndex = (int)(scrollView.contentOffset.x + SCREEN_WIDTH / 2) / SCREEN_WIDTH;
    if (cuttentIndex == self.images.count - 1) {
        if ([self isScrolltoLeft:scrollView]) {
            self.block();
        }
    }
}

#pragma mark - 判断滚动方向
- (BOOL)isScrolltoLeft:(UIScrollView *)scrollView{
    //返回YES为向左滚动，NO为右滚动
    if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x < 0) {
        return YES;
    }else{
        return NO;
    }
}

#pragma mark - lazyload
- (UIScrollView *)mainView {
    if (!_mainView) {
        _mainView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _mainView.showsHorizontalScrollIndicator = NO;
        _mainView.bounces = NO;
        _mainView.pagingEnabled = YES;
        _mainView.contentSize = CGSizeMake(self.images.count * SCREEN_WIDTH, 0);
        _mainView.delegate = self;
        for (int i = 0; i < self.images.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            imageView.backgroundColor = RGBRANDOMCOLOR;
            imageView.image = [UIImage imageNamed:self.images[i]];
            [_mainView addSubview:imageView];
            if (i == self.images.count - 1) {
                imageView.userInteractionEnabled = YES;
                UIButton *button = [UIButton buttonWithTitle:nil font:GZNormalFontSize titleColor:[UIColor blackColor] backgroundColor:[UIColor clearColor] haveCorner:YES];
                [imageView addSubview:button];
                //-60
                //x -75
                CGFloat  bottomOffset =  SCREEN_HEIGHT == 812 ? 75 : 60;
                button.frame = CGRectMake(AdjustWidth(50), SCREEN_HEIGHT - bottomOffset - AdjustWidth(50), SCREEN_WIDTH - AdjustWidth(100), AdjustWidth(50));
        
                @weakify(self);
                [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
                    @strongify(self);
                    self.loginBlock();
                }];
            }
        }
    }
    return _mainView;
}

- (UIPageControl *)page {
    if (!_page) {
        _page = [[UIPageControl alloc] init];
        _page.numberOfPages = self.images.count;
        _page.currentPage = 0;
        //        _page.defersCurrentPageDisplay = YES;
    }
    return _page;
}



@end
